package com.zs.admin.controller.wisdbook;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.google.common.collect.Maps;
import com.zs.admin.validate.wisdbook.WisdbookQuestValidate;
import com.zs.admin.vo.qd.QdQuestListedVo;
import com.zs.admin.vo.wisdbook.WisdbookQuestListedVo;
import com.zs.common.aop.DevNotLogin;
import com.zs.common.core.AjaxResult;
import com.zs.common.util.StringUtils;
import com.zs.common.util.YmlUtils;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import net.dongliu.requests.RawResponse;
import net.dongliu.requests.Requests;
import org.apache.commons.compress.utils.Lists;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;


@Slf4j
@RestController
@RequestMapping("api/wisdbook/page/quest")
public class WisdbookQuestController {


    @DevNotLogin
    @PostMapping("/search")
    @ApiOperation(value="搜题")
    public AjaxResult questSearch(@RequestBody WisdbookQuestValidate questValidate) {


        if(StringUtils.isEmpty(questValidate.getText())&&StringUtils.isEmpty(questValidate.getMarkId())){
            return AjaxResult.failed("参数有误");
        }
        HashMap<String, String> params = Maps.newHashMap();
        String ocrText=questValidate.getText();
//        if (StringUtils.isEmpty(questValidate.getText())){
//            String markId = questValidate.getMarkId();
//            String markImg=YmlUtils.get("sysconf.mark-render-url")+"?markId="+markId;
//
//            String ocrByUrl = YmlUtils.get("zs.ocrByUrl");
//
//            params.put("imgUrl",markImg);
//            String ocrRet = Requests.post(ocrByUrl).body(params).send().readToText();
//            JSONObject js = (JSONObject) JSON.parse(ocrRet);
//            ocrText = js.getJSONObject("data").getString("content");
//        }
        {

            String markId = questValidate.getMarkId();
            String markImg=YmlUtils.get("sysconf.mark-render-url")+"?markId="+markId;

            String ocrByUrl = YmlUtils.get("zs.ocrByUrl");

            params.put("imgUrl",markImg);
            String ocrRet = Requests.post(ocrByUrl).body(params).send().readToText();
            JSONObject js = (JSONObject) JSON.parse(ocrRet);
            ocrText = js.getJSONObject("data").getString("content");
        }

        log.info(ocrText);

        String textSearchUrl = YmlUtils.get("zs.textSearch");

        params.clear();
        params.put("text",ocrText);
        String  ocrRet = Requests.post(textSearchUrl).body(params).send().readToText();
        JSONObject js = (JSONObject) JSON.parse(ocrRet);
        List<WisdbookQuestListedVo> lists = Lists.newArrayList();
        if(js.getInteger("code") != 0){
            return AjaxResult.success(lists);
        }

        JSONArray jsonArray = js.getJSONObject("data").getJSONArray("questList");

        for (Object j: jsonArray) {
            JSONObject jo = (JSONObject) j;
            WisdbookQuestListedVo qdQuestListedVo = new WisdbookQuestListedVo();

            qdQuestListedVo.setId(jo.getString("id"));
            qdQuestListedVo.setQuestion(jo.getString("question"));
            qdQuestListedVo.setAnswer(jo.getString("answer"));
            qdQuestListedVo.setSource(jo.getInteger("source"));

            lists.add(qdQuestListedVo);
        }

        return AjaxResult.success(lists);
    }
}
